package com.zili.multistreamconversion;

import com.zili.source.ClickSource;
import com.zili.source.Event;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author : ranzlupup
 * @since : 2023/3/14 08:43
 */
public class SplitStreamByFilter {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DataStreamSource<Event> stream = env.addSource(new ClickSource());

        // 筛选Mary的浏览行为放入MaryStream流中
        SingleOutputStreamOperator<Event> maryStream = stream.filter(
                new FilterFunction<Event>() {
                    @Override
                    public boolean filter(Event value) throws Exception {
                        return value.user.equals("Mary");

                    }
                }
        );
        // 筛选 Bob 的购买行为放入 BobStream 流中
        SingleOutputStreamOperator<Event> bobStream = stream.filter(new FilterFunction<Event>() {
            @Override
            public boolean filter(Event value) throws Exception {
                return value.user.equals("Bob");
            }
        });
        // 筛选其他人的浏览行为放入 elseStream 流中
        SingleOutputStreamOperator<Event> elseStream = stream.filter(new FilterFunction<Event>() {
            @Override
            public boolean filter(Event value) throws Exception {
                return !value.user.equals("Mary") && !value.user.equals("Bob");
            }
        });

        maryStream.print("Mary: ");
        bobStream.print("Bob: ");
        elseStream.print("else: ");


        env.execute();
    }
}
